package com.xunyin.modules.mtstore.service.mapper;

import com.xunyin.common.mapper.CoreMapper;
import com.xunyin.modules.mtstore.domain.MtStoreRoom;
import com.xunyin.modules.mtstore.vo.MtStoreAndRoomVo;
import com.xunyin.modules.mtstore.vo.MtStoreRoomVo;
import com.xunyin.modules.shareorder.service.dto.ShareCountDto;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 包间 Mapper 接口
 * <p>
 * Created by xunyinyunshang <3594419785@qq.com>
 * CopyRight 讯音云商 https://www.tonetn.com
 */
@Repository
public interface MtStoreRoomMapper extends CoreMapper<MtStoreRoom> {

    @Select("SELECT stc.id AS roomId, " +
            "stc.store_room_name AS storeRoomName, " +
            "stc.price, " +
            "stc.repeat_price as repeatPrice, " +
            "stc.recommended, " +
            "stc.icon AS icon, " +
            "stc.taocan_list as taocanList, " +
            "stc.images AS images, " +
            "stc.store_room_status AS storeRoomStatus, " +
            "stc.server_end_time AS serverEndTime, " +
            "stc.service_fac_ids AS serviceFacIds " +
            "FROM mt_store_room stc " +
            "WHERE stc.store_id = #{storeId} and stc.is_del=0 AND stc.room_online_status = 'A' ")
    List<MtStoreRoomVo> getStoreRoomList(@Param("storeId") Integer storeId);


    @Select("SELECT stc.id AS roomId, " +
            "stc.store_room_name AS storeRoomName, " +
            "stc.price, " +
            "stc.repeat_price as repeatPrice, " +
            "stc.recommended, " +
            "stc.icon AS icon, " +
            "stc.taocan_list as taocanList, " +
            "stc.images AS images, " +
            "stc.store_room_status AS storeRoomStatus, " +
            "stc.server_end_time AS serverEndTime, " +
            "stc.service_fac_ids AS serviceFacIds, " +
            "stc.room_online_status AS roomOnlineStatus " +
            "FROM mt_store_room stc " +
            "WHERE stc.store_id = #{storeId} and stc.is_del=0  ")
    List<MtStoreRoomVo> getStoreRoomListForAdmin(@Param("storeId") Integer storeId);


    @Select("SELECT stc.id AS roomId, " +
            "stc.store_room_name AS storeRoomName, " +
            "stc.price, " +
            "stc.repeat_price as repeatPrice, " +
            "t.start_time_num AS startTimeNum, " +
            "stc.recommended, " +
            "stc.icon AS icon, " +
            "stc.images AS images, " +
            "stc.store_room_status AS storeRoomStatus, " +
            "stc.server_end_time AS serverEndTime, " +
            "stc.service_fac_ids AS serviceFacIds, " +
            "t.longitude AS longitude, " +
            "t.latitude AS latitude, " +
            "t.address AS address, " +
            "t.phone AS phone," +
            "t.id as storeId, " +
            "stc.taocan_list as taocanList, " +
            "t.name as storeName, " +
            "(6371 * ACOS(COS( RADIANS(#{latitude}))*COS(RADIANS(t.latitude))*COS(RADIANS(t.longitude ) - RADIANS(#{longitude})) + SIN(RADIANS(#{latitude}))*SIN(RADIANS(t.latitude)))) AS distance " +
            "FROM mt_store_room stc " +
            "left join mt_store t on stc.store_id=t.id " +
            "WHERE stc.id = #{storeRoomId} and stc.is_del=0")
    MtStoreRoomVo getStoreRoomDetail(@Param("storeRoomId") Integer storeRoomId, @Param("latitude") String latitude, @Param("longitude") String longitude);

    @Select("SELECT stc.id AS roomId, " +
            "stc.store_room_status AS storeRoomStatus " +
            "FROM mt_store_room stc " +
            "WHERE stc.id = #{storeRoomId} ")
    MtStoreRoomVo getStoreRoomInfor(@Param("storeRoomId") Integer storeRoomId);

    /**
     * 修改房间状态
     *
     * @param storeRoomId     房间Id
     * @param storeRoomStatus 房间状态标识
     */
    @Update("update `mt_store_room` set store_room_status = #{storeRoomStatus},server_end_time=#{date} where id = #{storeRoomId}")
    void updateStoreRoomStatus(@Param("storeRoomStatus") Integer storeRoomStatus, @Param("date") Date date, @Param("storeRoomId") Integer storeRoomId);


    /**
     * 修改房间状态1
     *
     * @param storeRoomId     房间Id
     * @param storeRoomStatus 房间状态标识
     */
    @Update("update `mt_store_room` set store_room_status = #{storeRoomStatus} where id = #{storeRoomId} ")
    void updateStoreRoomStatus1(@Param("storeRoomStatus") Integer storeRoomStatus, @Param("storeRoomId") Integer storeRoomId);

    @Select("<script> " +
            " SELECT t2.store_room_name as roomName from t_share_order t1 " +
            " LEFT JOIN mt_store_room t2 on t1.store_room_id=t2.id " +
            " where t1.order_status in (0,1,2,4) " +
            " <if test =\"storeId !=null\"> and t1.store_id = #{storeId} </if> " +
            " </script>")
    List<ShareCountDto> findRoomName(@Param("storeId") Integer storeId);


    @Select("SELECT stc.id AS storeRoomId, " +
            "stc.store_room_name AS storeRoomName, " +
            "stc.store_id as storeId, " +
            "a.name as storeName, " +
            "stc.price, " +
            "stc.repeat_price as repeatPrice, " +
            "t.start_time_num AS startTimeNum, " +
            "stc.recommended, " +
            "stc.icon AS icon, " +
            "stc.images AS images, " +
            "a.phone as phone " +
            "FROM mt_store_room stc " +
            "left join mt_store a on stc.store_id=a.id " +
            "WHERE stc.store_id = #{storeId} and stc.id = #{storeRoomId} and stc.is_del=0  ")
    MtStoreAndRoomVo getStoreAndRoom(@Param("storeId") Integer storeId, @Param("storeRoomId") Integer storeRoomId);


}
